using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_BinhLuanUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack )
        {
            LoadData();
            LoadDropDownList();
           
        }
        txt_ngaygui.Text = DateTime.Now.ToString("dd/MM/yyyy");

    }
    private void LoadDropDownList()
    {
        tbKhachHangCollection khCollection = tbKhachHangBUS.GetAll();
        ddl_khachhang.DataTextField = tbKhachHangColumns.HoTen.ToString();
        ddl_khachhang.DataValueField = tbKhachHangColumns.MaKH.ToString();
        ddl_khachhang.DataSource = khCollection;
        ddl_khachhang.DataBind();
        ddl_khachhang.Items.Insert(0, new ListItem("All", "0"));

        tbSanPhamCollection spCollection = tbSanPhamBUS.GetAll();
        ddl_sanpham.DataTextField = tbSanPhamColumns.TenSP.ToString();
        ddl_sanpham.DataValueField = tbSanPhamColumns.MaSP.ToString();
        ddl_sanpham.DataSource = spCollection;
        ddl_sanpham.DataBind();
        ddl_sanpham.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        try
        {
            int iD = int.Parse(Request.QueryString["iD"].ToString ());
            txt_mabinhluan.Text = iD.ToString();
            tbBinhLuan binhluan=tbBinhLuanBUS .GetByID (iD); 
            ddl_khachhang.SelectedValue = binhluan.KhachHang.ToString();
            ddl_sanpham.SelectedValue = binhluan.SanPham.ToString();
            txt_noidung.Text = binhluan.NoiDung;
            txt_ngaygui.Text = binhluan.NgayGui.ToString ();
        }
        catch
        {
            Response.Redirect("~/Admin/BinhLuanDanhSach.aspx");
        }

    }
    protected void btn_sua_Click(object sender, EventArgs e)
    {
        try
        {
            int iD = int.Parse(Request.QueryString["iD"].ToString ());
            tbBinhLuan binhluan = tbBinhLuanBUS.GetByID(iD);
            binhluan.KhachHang = int.Parse(ddl_khachhang.SelectedValue);
            binhluan.SanPham = int.Parse(ddl_sanpham.SelectedValue);
            binhluan.NoiDung = txt_noidung.Text;
            binhluan.NgayGui =DateTime .Parse ( txt_ngaygui.Text);
            tbBinhLuanBUS.Update(binhluan);
            Response.Redirect("~/Admin/BinhLuanDanhSach.aspx");
        }
        catch
        {
            Response.Redirect("~/Admin/BinhLuanDanhSach.aspx");
        }

            Response.Redirect(Request.RawUrl);
    }
}
